package com.baizhi.serviceimpl;

import com.baizhi.dao.LogMapper;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Log;
import com.baizhi.entity.LogExample;
import com.baizhi.service.LogService;
import com.baizhi.util.UUIDUtil;
import com.baizhi.vo.CommonQueryPageVo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class LogServiceImpl implements LogService {

    @Resource
    LogMapper logMapper;

    @Override
    public CommonQueryPageVo queryAllPage(PageDTO pageDTO) {

        LogExample example = new LogExample();

        example.setOrderByClause("option_time desc");

        int count = logMapper.selectCountByExample(example);

        RowBounds bounds = new RowBounds((pageDTO.getPage() - 1) * pageDTO.getPageSize(), pageDTO.getPageSize());

        List<Log> logs = logMapper.selectByExampleAndRowBounds(example, bounds);

        return new CommonQueryPageVo(pageDTO.getPage(),count,logs);
    }

    //以非事务的方式运行，如果当前存在事务则挂起事务
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public void add(Log log) {
        log.setId(UUIDUtil.getUUID());

        logMapper.insertSelective(log);
    }
}
